package com.sxfq.loansupermarket.backsystem.modular.marketing.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.marketing.dao.ReservationsArrivedDao;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.SearchReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.IReservationsArrivedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ReservationsArrivedServiceImpl implements IReservationsArrivedService {

    @Autowired
    private ReservationsArrivedDao dao;

    /**
     * 已到  界面表格数据
     *
     * @param page
     * @param searchInfo
     * @return
     */
    public List<ReservationsInfo> selectPageListByStatusMapYD(Page<ReservationsInfo> page, SearchReservationsInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStore()) && !"请选择".equals(searchInfo.getArrivalStore())) {
            String arrivalStore = "";
            String arrivalStore2[] = searchInfo.getArrivalStore().split(",");
            if (arrivalStore2.length == 1) {
                arrivalStore = arrivalStore2[0];
            } else {
                for (int i = 0; i < arrivalStore2.length; i++) {
                    if (i == (arrivalStore2.length - 1)) {
                        arrivalStore += "'" + arrivalStore2[i];
                    } else if (i == 0) {
                        arrivalStore += arrivalStore2[i] + "'" + ",";
                    } else {
                        arrivalStore += "'" + arrivalStore2[i] + "'" + ",";
                    }
                }
            }
            params.put("arrivalStore", " t.arrival_store in ( '" + arrivalStore + "') ");
        }
        if (StringUtil.isNotEmpty(searchInfo.getRechargeStore())) {
            params.put("rechargeStore", searchInfo.getRechargeStore());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            params.put("dealStatus", searchInfo.getDealStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDepartment())) {
            params.put("department", " t.follower in (select t1.account from user t1 where  t1.roleid='"+searchInfo.getDepartment()+"') ");
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getArea())) {
            String area = "";
            String area2[] = searchInfo.getArea().split(",");
            if (area2.length == 1) {
                area = area2[0];
            } else {
                for (int i = 0; i < area2.length; i++) {
                    if (i == (area2.length - 1)) {
                        area += "'" + area2[i];
                    } else if (i == 0) {
                        area += area2[i] + "'" + ",";
                    } else {
                        area += "'" + area2[i] + "'" + ",";
                    }
                }
            }
            params.put("area", " t.arrival_store  in (select a.simplename from dept a where a.area_abbreviation in ('" + area + "'))");
        }
        if (StringUtil.isNotEmpty(searchInfo.getAppointmentPayService())) {
            params.put("appointmentPayService", searchInfo.getAppointmentPayService());
        }
        if (StringUtil.isNotEmpty(searchInfo.getArchiving())) {
            if (searchInfo.getArchiving().indexOf(",") < 0) {
                String str = " ('" + searchInfo.getArchiving() + "') ";
                params.put("archiving", str);
            } else {
                String[] archivings = searchInfo.getArchiving().split(",");
                String str = " (";
                for (int i = 0; i < archivings.length; i++) {
                    if (i == archivings.length - 1) {
                        str += "'" + archivings[i] + "'";
                    } else {
                        str += "'" + archivings[i] + "',";
                    }
                }
                str += ") ";
                params.put("archiving", str);
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollower())) {
            params.put("follower", searchInfo.getFollower());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAccessDisease())) {
            params.put("accessDisease", searchInfo.getAccessDisease());
        }
        if (!"请选择".equals(searchInfo.getOneChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getTwoChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getThrChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }

        if (StringUtil.isNotEmpty(searchInfo.getStatuss()) && !"请选择".equals(searchInfo.getStatuss())) {
            String statuss = "";
            String statuss2[] = searchInfo.getStatuss().split(",");
            if (statuss2.length == 1) {
                statuss = statuss2[0];
            } else {
                for (int i = 0; i < statuss2.length; i++) {
                    if (i == (statuss2.length - 1)) {
                        statuss += "'" + statuss2[i];
                    } else if (i == 0) {
                        statuss += statuss2[i] + "'" + ",";
                    } else {
                        statuss += "'" + statuss2[i] + "'" + ",";
                    }
                }
            }
            params.put("statuss", " t.statuss in ( '" + statuss + "') ");
        }


        if (StringUtil.isNotEmpty(searchInfo.getStatus())) {
            params.put("status", searchInfo.getStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getBelonger())) {
            params.put("belonger", searchInfo.getBelonger());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartFirstVisitDate())) {
            params.put("startFirstVisitDate", searchInfo.getStartFirstVisitDate());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndFirstVisitDate())) {
            params.put("endFirstVisitDate", searchInfo.getEndFirstVisitDate());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTransactionTime())) {
            params.put("startTransactionTime", searchInfo.getStartTransactionTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTransactionTime())) {
            params.put("endTransactionTime", searchInfo.getEndTransactionTime());
        }
        params.put("condition1", "order by t.first_visit_date desc");
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPagesYD(page, params);
    }


    public int getQueryPagesCountYD(Page<ReservationsInfo> page, SearchReservationsInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStore()) && !"请选择".equals(searchInfo.getArrivalStore())) {
            String arrivalStore = "";
            String arrivalStore2[] = searchInfo.getArrivalStore().split(",");
            if (arrivalStore2.length == 1) {
                arrivalStore = arrivalStore2[0];
            } else {
                for (int i = 0; i < arrivalStore2.length; i++) {
                    if (i == (arrivalStore2.length - 1)) {
                        arrivalStore += "'" + arrivalStore2[i];
                    } else if (i == 0) {
                        arrivalStore += arrivalStore2[i] + "'" + ",";
                    } else {
                        arrivalStore += "'" + arrivalStore2[i] + "'" + ",";
                    }
                }
            }
            params.put("arrivalStore", " t.arrival_store in ( '" + arrivalStore + "') ");
        }
        if (StringUtil.isNotEmpty(searchInfo.getRechargeStore())) {
            params.put("rechargeStore", searchInfo.getRechargeStore());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            params.put("dealStatus", searchInfo.getDealStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getArea())) {
            String area = "";
            String area2[] = searchInfo.getArea().split(",");
            if (area2.length == 1) {
                area = area2[0];
            } else {
                for (int i = 0; i < area2.length; i++) {
                    if (i == (area2.length - 1)) {
                        area += "'" + area2[i];
                    } else if (i == 0) {
                        area += area2[i] + "'" + ",";
                    } else {
                        area += "'" + area2[i] + "'" + ",";
                    }
                }
            }
            params.put("area", " t.arrival_store  in (select a.simplename from dept a where a.area_abbreviation in ('" + area + "'))");
        }
        if (StringUtil.isNotEmpty(searchInfo.getDepartment())) {
            params.put("department", " t.follower in (select t1.account from user t1 where  t1.roleid='"+searchInfo.getDepartment()+"') ");
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAppointmentPayService())) {
            params.put("appointmentPayService", searchInfo.getAppointmentPayService());
        }
        if (StringUtil.isNotEmpty(searchInfo.getArchiving())) {
            if (searchInfo.getArchiving().indexOf(",") < 0) {
                String str = " ('" + searchInfo.getArchiving() + "') ";
                params.put("archiving", str);
            } else {
                String[] archivings = searchInfo.getArchiving().split(",");
                String str = " (";
                for (int i = 0; i < archivings.length; i++) {
                    if (i == archivings.length - 1) {
                        str += "'" + archivings[i] + "'";
                    } else {
                        str += "'" + archivings[i] + "',";
                    }
                }
                str += ") ";
                params.put("archiving", str);
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollower())) {
            params.put("follower", searchInfo.getFollower());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAccessDisease())) {
            params.put("accessDisease", searchInfo.getAccessDisease());
        }
        if (!"请选择".equals(searchInfo.getOneChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getTwoChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getThrChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getStatuss()) && !"请选择".equals(searchInfo.getStatuss())) {
            String statuss = "";
            String statuss2[] = searchInfo.getStatuss().split(",");
            if (statuss2.length == 1) {
                statuss = statuss2[0];
            } else {
                for (int i = 0; i < statuss2.length; i++) {
                    if (i == (statuss2.length - 1)) {
                        statuss += "'" + statuss2[i];
                    } else if (i == 0) {
                        statuss += statuss2[i] + "'" + ",";
                    } else {
                        statuss += "'" + statuss2[i] + "'" + ",";
                    }
                }
            }
            params.put("statuss", " t.statuss in ( '" + statuss + "') ");
        }
        if (StringUtil.isNotEmpty(searchInfo.getStatus())) {
            params.put("status", searchInfo.getStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getBelonger())) {
            params.put("belonger", searchInfo.getBelonger());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartFirstVisitDate())) {
            params.put("startFirstVisitDate", searchInfo.getStartFirstVisitDate());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndFirstVisitDate())) {
            params.put("endFirstVisitDate", searchInfo.getEndFirstVisitDate());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTransactionTime())) {
            params.put("startTransactionTime", searchInfo.getStartTransactionTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTransactionTime())) {
            params.put("endTransactionTime", searchInfo.getEndTransactionTime());
        }
        params.put("condition1", "order by t.first_visit_date desc");
        return dao.getQueryPagesCountYD(page, params);
    }

    public List<ReservationsInfo> reservationsExportYD(SearchReservationsInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStore()) && !"请选择".equals(searchInfo.getArrivalStore())) {
            String arrivalStore = "";
            String arrivalStore2[] = searchInfo.getArrivalStore().split(",");
            if (arrivalStore2.length == 1) {
                arrivalStore = arrivalStore2[0];
            } else {
                for (int i = 0; i < arrivalStore2.length; i++) {
                    if (i == (arrivalStore2.length - 1)) {
                        arrivalStore += "'" + arrivalStore2[i];
                    } else if (i == 0) {
                        arrivalStore += arrivalStore2[i] + "'" + ",";
                    } else {
                        arrivalStore += "'" + arrivalStore2[i] + "'" + ",";
                    }
                }
            }
            params.put("arrivalStore", " t.arrival_store in ( '" + arrivalStore + "') ");
        }
        if (StringUtil.isNotEmpty(searchInfo.getRechargeStore())) {
            params.put("rechargeStore", searchInfo.getRechargeStore());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            params.put("dealStatus", searchInfo.getDealStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDepartment())) {
            params.put("department", " t.follower in (select t1.account from user t1 where  t1.roleid='"+searchInfo.getDepartment()+"') ");
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getArea())) {
            String area = "";
            String area2[] = searchInfo.getArea().split(",");
            if (area2.length == 1) {
                area = area2[0];
            } else {
                for (int i = 0; i < area2.length; i++) {
                    if (i == (area2.length - 1)) {
                        area += "'" + area2[i];
                    } else if (i == 0) {
                        area += area2[i] + "'" + ",";
                    } else {
                        area += "'" + area2[i] + "'" + ",";
                    }
                }
            }
            params.put("area", " t.arrival_store  in (select a.simplename from dept a where a.area_abbreviation in ('" + area + "'))");
        }
        if (StringUtil.isNotEmpty(searchInfo.getAppointmentPayService())) {
            params.put("appointmentPayService", searchInfo.getAppointmentPayService());
        }
        if (StringUtil.isNotEmpty(searchInfo.getArchiving())) {
            if (searchInfo.getArchiving().indexOf(",") < 0) {
                String str = " ('" + searchInfo.getArchiving() + "') ";
                params.put("archiving", str);
            } else {
                String[] archivings = searchInfo.getArchiving().split(",");
                String str = " (";
                for (int i = 0; i < archivings.length; i++) {
                    if (i == archivings.length - 1) {
                        str += "'" + archivings[i] + "'";
                    } else {
                        str += "'" + archivings[i] + "',";
                    }
                }
                str += ") ";
                params.put("archiving", str);
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollower())) {
            params.put("follower", searchInfo.getFollower());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAccessDisease())) {
            params.put("accessDisease", searchInfo.getAccessDisease());
        }
        if (!"请选择".equals(searchInfo.getOneChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getTwoChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getThrChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }

        if (StringUtil.isNotEmpty(searchInfo.getStatuss()) && !"请选择".equals(searchInfo.getStatuss())) {
            String statuss = "";
            String statuss2[] = searchInfo.getStatuss().split(",");
            if (statuss2.length == 1) {
                statuss = statuss2[0];
            } else {
                for (int i = 0; i < statuss2.length; i++) {
                    if (i == (statuss2.length - 1)) {
                        statuss += "'" + statuss2[i];
                    } else if (i == 0) {
                        statuss += statuss2[i] + "'" + ",";
                    } else {
                        statuss += "'" + statuss2[i] + "'" + ",";
                    }
                }
            }
            params.put("statuss", " t.statuss in ( '" + statuss + "') ");
        }


        if (StringUtil.isNotEmpty(searchInfo.getStatus())) {
            params.put("status", searchInfo.getStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getBelonger())) {
            params.put("belonger", searchInfo.getBelonger());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartFirstVisitDate())) {
            params.put("startFirstVisitDate", searchInfo.getStartFirstVisitDate());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndFirstVisitDate())) {
            params.put("endFirstVisitDate", searchInfo.getEndFirstVisitDate());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTransactionTime())) {
            params.put("startTransactionTime", searchInfo.getStartTransactionTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTransactionTime())) {
            params.put("endTransactionTime", searchInfo.getEndTransactionTime());
        }
        params.put("condition1", "order by t.first_visit_date desc");
        return dao.getQueryPageExportYD(params);
    }
}